﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage.ManagerCategory
{
    public partial class CategoryEdit : System.Web.UI.Page
    {
        public string checkstr = null;
        public static int id = 0;
        public int check = 0;
        public string newcheck = null;
        public int protypeId = 0;
        public Category ca = new Category();
        CategoryBLL cateBll = new CategoryBLL();
        public IList<Category> catePrList = new List<Category>();
        

        ProdTypeBLL prodTypeBll = new ProdTypeBLL();
        public IList<ProdType> typeList = new List<ProdType>();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                catePrList = cateBll.FindListPro();                
                typeList = prodTypeBll.FindAll();
                try
                {
                    LoadEdit();
                }
                catch
                {

                }
            }
        }
        private void LoadEdit()
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            ca = (Category)cateBll.FindModelById("Category", id);
            check = Convert.ToInt32(Request.Form.Get("yes"));
            protypeId = ca.GoodsType;
            if (ca.IsShow)
            {
                checkstr = "checked=\"checked\"";
            }
        }
        protected void btnSave_Click1(object sender, EventArgs e)
        {
            int GoodsType = 0;
            int id = int.Parse(Request["id"]);
            int parentId = int.Parse(Request["ParentId"]);
            string caName = StringUtil.checkStr(Request["CatName"]);          
            int SortOrder = int.Parse(Request["SortOrder"]);
            string MeasureUnit = StringUtil.checkStr(Request["MeasureUnit"]);
            string IsShow = Request["yes"];
            string CatDesc =Request["CatDesc"];
            string KeyWord = StringUtil.checkStr(Request["KeyWord"]);
            if (!string.IsNullOrEmpty(Request["GoodsType"]))
            {
                GoodsType = int.Parse(Request["GoodsType"]);
            }
            caName = caName.Trim().Replace(" ", "");
            cateBll.entity = cateBll.FindModelByName(parentId, caName);
            if (cateBll.entity != null)
            {
               if (cateBll.entity.CatName.Equals(caName)&&cateBll.entity.Id!=id)
                {
                     StringUtil.Alert("此类别已存在！", "-1");
                }
               else
               {
                   Category ca = new Category();
                   ca.Id = id;
                   ca.ParentId = parentId;
                   ca.CatName = caName;
                   ca.KeyWord = KeyWord;
                   ca.MeasureUnit = MeasureUnit;
                   ca.SortOrder = SortOrder;
                   ca.GoodsType = GoodsType;
                   ca.IsShow = IsShow == "true" ? true : false;
                   ca.CatDesc = CatDesc;
                   cateBll.entity = ca;
                   if (cateBll.UpDate())
                   {
                       StringUtil.Alert("更新成功！", "CategoryList.aspx");
                   }
                   else
                   {
                       StringUtil.Alert("更新失败！", "-1");

                   }
                   
               }
               
            }
            else
            {
                Category ca = new Category();
                ca.Id = id;
                ca.ParentId = parentId;
                ca.CatName = caName;
                ca.KeyWord = KeyWord;
                ca.MeasureUnit = MeasureUnit;
                ca.SortOrder = SortOrder;
                ca.GoodsType = GoodsType;
                ca.IsShow = IsShow == "true" ? true : false;
                ca.CatDesc = CatDesc;
                cateBll.entity = ca;
                if (cateBll.UpDate())
                {
                    StringUtil.Alert("更新成功！", "CategoryList.aspx");
                }
                else
                {
                    StringUtil.Alert("更新失败！", "-1");

                }
            }
        }       
    }
}
